<template>
  <div class="qrcode-tool">
    <Breadcrumb />
    
    <div class="tool-header">
      <div class="container">
        <h1>二维码生成器</h1>
        <div class="subtitle-group">
          <p class="subtitle">在线生成自定义二维码，支持文本、链接等内容</p>
        </div>
      </div>
    </div>

    <div class="tool-content">
      <div class="container">
        <div class="qrcode-panel">
          <div class="input-section">
            <h3>输入内容</h3>
            <textarea 
              v-model="qrContent" 
              placeholder="请输入要生成二维码的内容..."
              @input="generateQrCode"
            ></textarea>
          </div>
          <div class="output-section">
            <h3>生成的二维码</h3>
            <div class="qrcode-display" v-if="qrCodeDataUrl">
              <img :src="qrCodeDataUrl" alt="生成的二维码" class="qrcode-image">
            </div>
            <button 
              class="download-btn" 
              @click="downloadQrCode"
              :disabled="!qrCodeDataUrl"
            >
              下载二维码
            </button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import Breadcrumb from '@/components/Breadcrumb.vue'
import QRCode from 'qrcode'

const qrContent = ref('')
const qrCodeDataUrl = ref('')

// 生成二维码
const generateQrCode = async () => {
  if (!qrContent.value.trim()) {
    qrCodeDataUrl.value = ''
    return
  }
  try {
    qrCodeDataUrl.value = await QRCode.toDataURL(qrContent.value, { errorCorrectionLevel: 'H', width: 200, margin: 2 })
  } catch (error) {
    console.error('二维码生成失败:', error)
  }
}

// 下载二维码
const downloadQrCode = () => {
  if (!qrCodeDataUrl.value) return
  const a = document.createElement('a')
  a.href = qrCodeDataUrl.value
  a.download = 'qrcode.png'
  document.body.appendChild(a)
  a.click()
  document.body.removeChild(a)
}
</script>

<style scoped lang="scss">
.qrcode-tool {
  .tool-header {
    background: linear-gradient(135deg, var(--primary-color), var(--hover-color));
    padding: 20px 0;
    color: white;
    text-align: center;
    margin-top: -1px;

    h1 {
      font-size: 1.8rem;
      margin-bottom: 0.5rem;
    }

    .subtitle-group {
      display: flex;
      flex-direction: column;
      gap: 8px;
      align-items: center;

      .subtitle {
        font-size: 0.95rem;
        opacity: 0.9;
        background: rgba(255, 255, 255, 0.1);
        padding: 6px 16px;
        border-radius: 20px;
        backdrop-filter: blur(10px);
      }
    }
  }

  .tool-content {
    padding: 40px 0;

    .qrcode-panel {
      display: flex;
      gap: 40px;
      background: white;
      border-radius: 12px;
      box-shadow: 0 5px 15px rgba(0,0,0,0.1);
      padding: 30px;

      .input-section, .output-section {
        flex: 1;
      }

      .input-section {
        textarea {
          width: 100%;
          height: 200px;
          padding: 15px;
          border: 1px solid #eee;
          border-radius: 6px;
          font-family: 'Monaco', monospace;
          font-size: 0.9rem;
          line-height: 1.5;
          resize: vertical;
          color: #333;

          &:focus {
            outline: none;
            border-color: var(--primary-color);
          }
        }
      }

      .output-section {
        .qrcode-display {
          margin-bottom: 20px;
          display: flex;
          justify-content: center;
          align-items: center;
          height: 200px;
          width: 200px;
          border: 1px solid #eee;
          border-radius: 6px;
          overflow: hidden;

          img {
            max-width: 100%;
            height: auto;
          }
        }

        .download-btn {
          padding: 10px 20px;
          border: none;
          background: var(--primary-color);
          color: white;
          border-radius: 6px;
          cursor: pointer;
          transition: background 0.3s ease;

          &:hover {
            background: var(--hover-color);
          }

          &:disabled {
            background: #ccc;
            cursor: not-allowed;
          }
        }
      }
    }
  }
}
</style> 